<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_errand\Entity\ShusyErrandOrder;
use Drupal\shusy_store_order\Entity\ShusyStoreOrder;

/**
 * @OA\Schema(
 *   schema="MyOrderItem",
 *   type="object",
 *   description="MyOrderItem",
 *   allOf={
 *       @OA\Schema(ref="#/components/schemas/OrderPayment"),
 *   }
 * )
 */
class MyOrderItem extends OrderPayment {

  protected function setValue() {
    parent::setValue();
    if ($this->entity instanceof ShusyStoreOrder) {
      $share_img_uri = variable_get_value('wx_mini_program:mini:share_img');
      $share_img = FALSE;
      if (!empty($share_img_uri) && is_file($share_img_uri)) {
        $share_img = image_style_url('wx_mini_program_share_img', $share_img_uri);
      }
      $title = variable_get_value('wx_mini_program:mini:name');
      $image = $share_img;
      $uid = $this->getUid();

      $path = "/preload/preload?&uid={$uid}";
      //订单类型 0 正常 1 拼团 2 秒杀
      switch ($this->entity->type) {
        case 1:
          $id = $this->wrapper->player->pintuan->product_id->value();
          $ptid = $this->wrapper->player->pintuan->id->value();
          $path = "/pages/preload/preload?&uid={$uid}&op=pintuan&id={$id}&type=3&ptid={$ptid}";
          break;
      }

      $value = [
        /**
         * @OA\Property(
         *   property="share",
         *   ref="#/components/schemas/OrderShare",
         *   description="订单分享信息",
         * ),
         */
        'share' => [
          'title' => $title,
          'image' => $image,
          'path' => $path,
        ],
      ];
      $this->merge($value);

      $value = [
        'errand' => FALSE,
      ];
      $this->merge($value);
      //附加快达信息
      $errand = $this->getErrand($this->entity);
      if ($errand instanceof ShusyErrandOrder && $errand->errand_id > 0) {
        $value = [
          /**
           * @OA\Property(
           *   property="errand",
           *   type="object",
           *   description="配送人员信息",
           *   ref="#/components/schemas/ErrandItem",
           * ),
           */
          'errand' => ErrandItem::get($errand->errand_id),
        ];
        $this->merge($value);
      }
    }
  }

  protected function getErrand(ShusyStoreOrder $order) {
    $es = shusy_errand_order_load_multiple(FALSE, ['store_order_id' => $order->id]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyErrandOrder) {
        return $e;
      }
    }
    return FALSE;
  }
}
